package ochart.elements.labels
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.text.TextField;
	
	import ochart.core.ChartObject;
	import ochart.utils.ArrayUtil;
	
	public class LabelBase extends ChartObject implements ILabel
	{
		public static const CLASS_NAME:String = "LabelBase"
		
		override public function get className():String
		{
			return CLASS_NAME;
		}
		
		public static const ALIAS:String = "labelBase";
		
		override public function get alias():String
		{
			return ALIAS;
		}
		
		public var autoSize:Boolean = true;
		
		override public function get measureHeight():Number
		{
			if(content is TextField)
			{
				return (content as TextField).textHeight;
			}
			return super.measureHeight;
		}
		
		override public function get measureWidth():Number
		{
			if(content is TextField)
			{
				return (content as TextField).textWidth;
			}
			return super.measureWidth;
		}
		
		private var _isTextChange:Boolean = false;

		public function get isTextChange():Boolean
		{
			return _isTextChange;
		}

		public function set isTextChange(value:Boolean):void
		{
			_isTextChange = value;
			if(value)
			{
				needRender = true;
			}
		}

		public var value:* = null;
		private var _text:String = null;
		
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:*):void
		{
			this.value = value;
			if(value == null || value === undefined)
			{
				_text = "";
			}
			else if((value is Number) && !isNaN(fractionFix))
			{
				_text = Number(value).toFixed(fractionFix);
			}
			else
			{
				try
				{
					_text = String(value);
				}
				catch(error:Error)
				{
					_text = "";
				}
			}
			
			isTextChange = true;
		}
		
		private var _multiline:Boolean = true;

		public function get multiline():Boolean
		{
			return _multiline;
		}

		public function set multiline(value:Boolean):void
		{
			_multiline = value;
			
			isTextChange = true;
		}
		
		override public function set color(value:Number):void
		{
			if(value < 0)
			{
				return;
			}
			super.color = value;
			
			isTextChange = true;
		}
		
		private var _size:Number = 12;

		public function get size():Number
		{
			return _size;
		}

		public function set size(value:Number):void
		{
			_size = value;
			
			isTextChange = true;
		}
		
		private var _fractionFix:Number = NaN;

		public function get fractionFix():Number
		{
			return _fractionFix;
		}

		public function set fractionFix(value:Number):void
		{
			_fractionFix = Math.ceil(value);
			
			isTextChange = true;
		}
		
		private var _bold:Boolean = false;
		
		public function get bold():Boolean
		{
			return _bold;
		}
		
		public function set bold(value:Boolean):void
		{
			_bold = value;
			
			isTextChange = true;
		}
		
		private var _italic:Boolean = false;
		
		public function get italic():Boolean
		{
			return _italic;
		}
		
		public function set italic(value:Boolean):void
		{
			_italic = value;
			
			isTextChange = true;
		}
		
		private var _underline:Boolean = false;
		
		public function get underline():Boolean
		{
			return _underline;
		}
		
		public function set underline(value:Boolean):void
		{
			_underline = value;
			
			isTextChange = true;
		}
		
		public var content:DisplayObject = null;
		
		private var defaultValue:Object = 
		{
			"autoSize" : true,
			"height" : 20,
			"width" : 20,
			"text" : "",
			"bold" : false,
			"multiline" : true,
			"fractionFix" : NaN,
			"italic" : false,
			"underline" : false,
			"color" : 0x000000,
			"size" : 12
		}
			
		private var _propertiesArray:Array = null;
		
		override public function get propertiesArray():Array
		{
			if(_propertiesArray)
			{
				return _propertiesArray;
			}
			
			_propertiesArray = super.propertiesArray;
			for(var name:String in defaultValue)
			{
				if(!ArrayUtil.containsValue(_propertiesArray, name))
				{
					_propertiesArray.push(name);
				}
			}
			return _propertiesArray;
		}
		
		public function LabelBase()
		{
			super();
		}
		
		override public function getDefaultValue(name:String):*
		{
			return (defaultValue[name] === undefined) ? super.getDefaultValue(name) : defaultValue[name];
		}
		
		override public function update(callAfterUpdate:Boolean=true):Boolean
		{
			if(!super.update(false))
			{
				return false;
			}
			
			if(isTextChange)
			{
				isTextChange = false;
			}
			
			if(callAfterUpdate)
			{
				return afterUpdate();
			}
			return true;
		}
		
		override public function updateSize():void
		{
			super.updateSize();
			
//			if(content is TextField)
//			{
//				var textField:TextField = content as TextField;
//				if(!autoSize)
//				{
//					textField.height = height;
//					textField.width = width;
//				}
//			}
			
			if(!autoSize && content)
			{
				content.height = height;
				content.width = width;
			}
		}
	}
}